import myRequest from '../request.js'

import dicts from '@/config/dicts.js'

//微信自动登录
export const autoLogin = (data) => {
	let header = {
		"Content-Type": "application/json"
	}

	return new Promise((resolve, reject) => {
		myRequest(dicts.buyer + `/purchaser/permit/connect/miniProgram/auto-login`, 'post', data, header).then(
			res => {
				resolve(res)
			}).catch((err) => {
				reject(err)
			})
	})
}

//获取微信code
export const getWechatCode = () => {
	return new Promise((resolve, reject) => {
		wx.login({
			success (res) {
				resolve(res.code)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

//账号绑定微信
export const bindWechat = () => {
	return new Promise((resolve, reject) => {
		wx.login({
			success (res) {
				resolve(bind(res.code))
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

function urlencode (str) {
	str = (str + '').toString();

	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
		replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

function bind (code) {
	return new Promise((resolve, reject) => {
		myRequest(dicts.buyer + `/purchaser/permit/connect/bind?code=${code}&type=WECHAT_MP_OPEN_ID`, 'post').then(
			res => {
				resolve(res)
			}).catch((err) => {
				reject(err)
			})
	})
}


// 获取用户协议列表
export const getAgreementList = () => {
	return new Promise((resolve, reject) => {
		myRequest(dicts.essential + '/common/agreement', 'get').then(res => {
			resolve(res)
		}).catch((err) => {
			reject(err)
		})
	})
}

// 手机验证码
// LOGIN 登录,REGISTER 注册,FIND_USER 找回,UPDATE_PASSWORD 改密,WALLET_PASSWORD 钱包密码
export const getCode = (codeType, mobile) => {
	return new Promise((resolve, reject) => {
		myRequest(dicts.essential + `/common/common/sms/${codeType}/${mobile}`, 'get').then(res => {
			resolve(res)
		}).catch((err) => {
			reject(err)
		})
	})
}

// 获取采购商关联列表
export const getRelevances = () => {
	return new Promise((resolve, reject) => {
		myRequest(dicts.buyer + `/purchaser/purchaser/purchaser/get/relevances`, 'get').then(res => {
			resolve(res)
		}).catch((err) => {
			reject(err)
		})
	})
}

// 切换登录接口  
export const switchoverLogin = (val) => {
	return new Promise((resolve, reject) => {
		myRequest(dicts.buyer + `/purchaser/permit/member/switchover/login`, 'post', val).then(res => {
			resolve(res)
		}).catch((err) => {
			reject(err)
		})
	})
}

//
export const registerRefreshToken = (val) => {
	return new Promise((resolve, reject) => {
		myRequest(dicts.buyer + `/purchaser/permit/member/refresh/` + val, 'get').then(res => {
			resolve(res)
		}).catch((err) => {
			reject(err)
		})
	})
}
